package com.carb.controller.admin;

import com.carb.annotation.RequiredRoles;
import com.carb.constant.AuthorityConstant;
import com.carb.dto.BusinessAddDTO;
import com.carb.dto.BusinessPageQueryDTO;
import com.carb.result.PageResult;
import com.carb.result.Result;
import com.carb.service.IBusinessService;
import com.carb.vo.BusinessVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 业务管理 前端控制器
 * </p>
 *
 * @author XuChaoyang
 * @since 2025-01-18
 */
@RestController("adminBusinessController")
@RequestMapping("/admin/business")
@Tag(name = "业务管理相关接口")
@Slf4j
public class BusinessController {

    @Resource
    private IBusinessService businessService;

    /**
     * 新增业务
     * @param businessAddDTO 新增业务数据模型
     */
    @RequiredRoles({AuthorityConstant.ADMIN, AuthorityConstant.STORE_MANAGER, AuthorityConstant.CASHIER})
    @PostMapping("/add")
    @Operation(summary = "新增业务")
    public Result<String> addBusiness(@RequestBody BusinessAddDTO businessAddDTO) {
        log.info("新增业务：{}", businessAddDTO);
        businessService.addBusiness(businessAddDTO);
        log.info("新增业务成功！");
        return Result.success();
    }

    /**
     * 业务分页查询
     * @param businessPageQueryDTO 查询条件
     * @return 业务分页结果
     */
    @GetMapping("/page")
    @Operation(summary = "业务分页查询")
    public Result<PageResult<BusinessVO>> pageQuery(BusinessPageQueryDTO businessPageQueryDTO) {
        log.info("业务分页查询：{}", businessPageQueryDTO);
        PageResult<BusinessVO> pageResult = businessService.pageQuery(businessPageQueryDTO);
        log.info("业务分页查询成功！");
        return Result.success(pageResult);
    }

    /**
     * 根据业务id查询业务信息
     * @param id 业务id
     * @return 查询结果
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据业务id查询业务信息")
    public Result<BusinessVO> getBusinessById(@PathVariable @Parameter(description = "业务id") Long id) {
        log.info("根据业务id查询业务信息，业务id：{}", id);
        BusinessVO businessVO = businessService.getBusinessById(id);
        log.info("根据业务id查询业务信息成功！");
        return Result.success(businessVO);
    }

    /**
     * 业务接单
     * @param id 业务id
     */
    @RequiredRoles({AuthorityConstant.STORE_MANAGER, AuthorityConstant.WAITER})
    @PostMapping("/accept/{id}")
    @Operation(summary = "业务接单")
    public Result<String> acceptBusiness(@PathVariable @Parameter(description = "业务id") Long id) {
        log.info("业务接单，业务id：{}", id);
        businessService.acceptBusiness(id);
        log.info("业务接单成功！");
        return Result.success();
    }

    /**
     * 完成业务
     * @param id 业务id
     */
    @RequiredRoles({AuthorityConstant.ADMIN, AuthorityConstant.STORE_MANAGER, AuthorityConstant.CASHIER, AuthorityConstant.WAITER})
    @PutMapping("/complete/{id}")
    @Operation(summary = "完成业务")
    public Result<String> completeBusiness(@PathVariable @Parameter(description = "业务id") Long id) {
        log.info("完成业务：{}", id);
        businessService.completeBusiness(id);
        log.info("完成业务成功！");
        return Result.success();
    }

    /**
     * 业务退款
     * @param id 业务id
     */
    @RequiredRoles({AuthorityConstant.ADMIN, AuthorityConstant.STORE_MANAGER, AuthorityConstant.CASHIER})
    @PutMapping("/refund/{id}")
    @Operation(summary = "业务退款")
    public Result<String> refundBusiness(@PathVariable @Parameter(description = "业务id") Long id) {
        log.info("业务退款：{}", id);
        businessService.refundBusiness(id);
        log.info("业务退款成功！");
        return Result.success();
    }

}
